﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Expense.DataAccess;

namespace Expense.Tracker
{
    public class ExpenseTracker
    {
        #region "Declarations"
        ExpenseDataAccess eDataAccess;
        ExpenseDataQuery eDataQuery;
        Dictionary<string, string> eDictData;

        private const string cmInsert = "insert";
        private const string cmLoadtree = "loadtree";
        private const string cmLoaddisplay = "loaddisplay";
        private const string cmLoadgraph = "loadgraph";
        private const string cmSettings = "settings";
        private const string cmSaveSettings = "savesettings";
        private const string cmTypes = "types";
        private const string cmSaveTypes = "savetypes";

        private const string ConnectionValue = "ExpenseDB";
        #endregion

        #region "Properties"
        private int _expenseID;
        public int ExpenseID
        {
            get { return _expenseID; }
            set { _expenseID = value; }
        }

        private string _expenseType;
        public string ExpenseType
        {
            get { return _expenseType; }
            set { _expenseType = value; }
        }

        private DateTime _expenseDate;
        public DateTime ExpenseDate
        {
            get { return _expenseDate; }
            set { _expenseDate = value; }
        }

        private string _expenseReceipt;
        public string ExpenseReceipt
        {
            get { return _expenseReceipt; }
            set { _expenseReceipt = value; }
        }

        private decimal _expenseValue;
        public decimal ExpenseValue
        {
            get { return _expenseValue; }
            set { _expenseValue = value; }
        }

        private bool _settingsValue;
        public bool SettingsValue
        {
            get { return _settingsValue; }
            set { _settingsValue = value; }
        }

        private int _settingsID;
        public int SettingsID
        {
            get { return _settingsID; }
            set { _settingsID = value; }
        }

        #endregion

        #region "Constructor"
        public ExpenseTracker()
        {
            
        }

        public ExpenseTracker(DateTime expenseDate)
        {
            _expenseDate = expenseDate;
        }

        public ExpenseTracker(string typeValue)
        {
            _expenseType = typeValue;
        }

        public ExpenseTracker(string monthStart, string monthEnd)
        {
            _expenseType = monthStart;
            _expenseReceipt = monthEnd;
        }

        public ExpenseTracker(bool settingsValue, int settingsID)
        {
            _settingsValue = settingsValue;
            _settingsID = settingsID;
        }

        public ExpenseTracker(string expenseType, DateTime expenseDate, string expenseReceipt, decimal expenseValue)
        {
            _expenseType = expenseType;
            _expenseDate = expenseDate;
            _expenseReceipt = expenseReceipt;
            _expenseValue = expenseValue;
        }
        #endregion

        #region "Methods"
        public DataTable GetSettings()
        {
            eDataAccess = new ExpenseDataAccess();
            eDataQuery = new ExpenseDataQuery();
            eDictData = new Dictionary<string, string>();
            string eQuery = "";
            DataTable SettingsDB = new DataTable();

            eQuery = eDataQuery.ReturnQueryString(cmSettings, this);
            eDictData = eDataQuery.ReturnQueryDictionary(cmSettings, this);

            try
            {
                SettingsDB = eDataAccess.GetTableObjectFromQuery(ConnectionValue, eQuery, eDictData, CommandType.Text);
            }
            catch (Exception)
            {
                throw;
            }

            return SettingsDB;
        }

        public DataTable GetTreeExpense()
        {
            eDataAccess = new ExpenseDataAccess();
            eDataQuery = new ExpenseDataQuery();
            eDictData = new Dictionary<string, string>();
            string eQuery = "";
            DataTable treeDB = new DataTable();

            eQuery = eDataQuery.ReturnQueryString(cmLoadtree, this);
            eDictData = eDataQuery.ReturnQueryDictionary(cmLoadtree, this);

            try
            {
                treeDB = eDataAccess.GetTableObjectFromQuery(ConnectionValue, eQuery, eDictData, CommandType.Text);
            }
            catch (Exception)
            {
                throw;
            }

            return treeDB;
        }

        public DataTable GetDisplayExpense()
        {
            eDataAccess = new ExpenseDataAccess();
            eDataQuery = new ExpenseDataQuery();
            eDictData = new Dictionary<string, string>();
            string eQuery = "";
            DataTable displayDB = new DataTable();

            eQuery = eDataQuery.ReturnQueryString(cmLoaddisplay, this);
            eDictData = eDataQuery.ReturnQueryDictionary(cmLoaddisplay, this);

            try
            {
                displayDB = eDataAccess.GetTableObjectFromQuery(ConnectionValue, eQuery, eDictData, CommandType.Text);
            }
            catch (Exception)
            {
                throw;
            }

            return displayDB;
        }

        public DataTable GetGraphExpense()
        {
            eDataAccess = new ExpenseDataAccess();
            eDataQuery = new ExpenseDataQuery();
            eDictData = new Dictionary<string, string>();
            string eQuery = "";
            DataTable displayDB = new DataTable();

            eQuery = eDataQuery.ReturnQueryString(cmLoadgraph, this);
            eDictData = eDataQuery.ReturnQueryDictionary(cmLoadgraph, this);

            try
            {
                displayDB = eDataAccess.GetTableObjectFromQuery(ConnectionValue, eQuery, eDictData, CommandType.Text);
            }
            catch (Exception)
            {
                throw;
            }

            return displayDB;
        }

        public DataTable GetExpenseTypes()
        {
            eDataAccess = new ExpenseDataAccess();
            eDataQuery = new ExpenseDataQuery();
            eDictData = new Dictionary<string, string>();
            string eQuery = "";
            DataTable displayDB = new DataTable();

            eQuery = eDataQuery.ReturnQueryString(cmTypes, this);
            eDictData = eDataQuery.ReturnQueryDictionary(cmTypes, this);

            try
            {
                displayDB = eDataAccess.GetTableObjectFromQuery(ConnectionValue, eQuery, eDictData, CommandType.Text);
            }
            catch (Exception)
            {
                throw;
            }

            return displayDB;
        }

        public int SaveExpense()
        {
            eDataAccess = new ExpenseDataAccess();
            eDataQuery = new ExpenseDataQuery();
            eDictData = new Dictionary<string, string>();
            string eQuery = "";
            int success = 0;

            eQuery = eDataQuery.ReturnQueryString(cmInsert, this);
            eDictData = eDataQuery.ReturnQueryDictionary(cmInsert, this);

            try
            {
                success = eDataAccess.ExecuteNonQuery(ConnectionValue, eQuery, eDictData, CommandType.Text);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                eDataAccess = null;
            }

            return success;
        }

        public int SaveSettings()
        {
            eDataAccess = new ExpenseDataAccess();
            eDataQuery = new ExpenseDataQuery();
            eDictData = new Dictionary<string, string>();
            string eQuery = "";
            int success = 0;

            eQuery = eDataQuery.ReturnQueryString(cmSaveSettings, this);
            eDictData = eDataQuery.ReturnQueryDictionary(cmSaveSettings, this);

            try
            {
                success = eDataAccess.ExecuteNonQuery(ConnectionValue, eQuery, eDictData, CommandType.Text);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                eDataAccess = null;
            }

            return success;
        }

        public int SaveTypes()
        {
            eDataAccess = new ExpenseDataAccess();
            eDataQuery = new ExpenseDataQuery();
            eDictData = new Dictionary<string, string>();
            string eQuery = "";
            int success = 0;

            eQuery = eDataQuery.ReturnQueryString(cmSaveTypes, this);
            eDictData = eDataQuery.ReturnQueryDictionary(cmSaveTypes, this);

            try
            {
                success = eDataAccess.ExecuteNonQuery(ConnectionValue, eQuery, eDictData, CommandType.Text);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                eDataAccess = null;
            }

            return success;
        }
        #endregion
    }
}
